﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Presentation.Web.Admin.Areas.Open.Controllers
{
    public class ClientController : Infrastructure.Crosscutting.NetFramework.Controllers.OpenBaseController
    {
        Application.Admin.MobileClient.IMobileClientService clientService;

        Application.Admin.MobileClient.IMobileClientStartLogService startLogService;

        Infrastructure.Crosscutting.Authorize.IUserService userService;

        public ClientController(Application.Admin.MobileClient.IMobileClientService client,
            Application.Admin.MobileClient.IMobileClientStartLogService startLog,
            Infrastructure.Crosscutting.Authorize.IUserService user)
        {
            if (client == null)
            {
                throw new ArgumentNullException("clientService is null");
            }
            if (client == null)
            {
                throw new ArgumentNullException("startLogService is null");
            }
            if (user == null)
            {
                throw new ArgumentNullException("userService is null");
            }
            clientService = client;
            startLogService = startLog;
            userService = user;
        }

        public EmptyResult Start(string id, string type)
        {
            if (!string.IsNullOrEmpty(id) && id.Length == 64)
            {
                var client = clientService.GetList().Where(e => e.DeviceToken == id).FirstOrDefault();
                if (client == null)
                {
                    client = new Domain.Entity.MobileClient();
                    client.AddTime = System.DateTime.Now;
                    client.IpAddress = "127.0.0.1";
                    client.Name = type;
                    client.Remark = type;
                    client.SortNum = 0;
                    client.State = true;
                    client.DeviceToken = id;
                    clientService.Add(client, this.SessionUser());
                    if (client.Id > 0)
                    {
                        this.AddStartLog(client.Id);
                    }
                }
                else
                {
                    if (!client.State)
                    {
                        client.State = true;
                        clientService.Modify(client, userService.GetDefautlOperUser());
                    }
                    this.AddStartLog(client.Id);
                }
            }
            return new EmptyResult();
        }

        #region util

        protected void AddStartLog(int id)
        {
            var log = new Domain.Entity.MobileClientStartLog();
            log.AddTime = System.DateTime.Now;
            log.ClientId = id;
            log.IpAddress = "127.0.0.1";
            log.SortNum = 0;
            log.State = true;
            startLogService.Add(log, this.SessionUser());
        }

        protected Domain.Entity.User SessionUser()
        {
            var user = Session[Infrastructure.Crosscutting.Comm.Service.ConstService.SessionNameCurrentUser]
                 as Domain.Entity.User;
            user = userService.GetDefautlOperUser();
            return user;
        }

        #endregion
    }
}
